from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from .models import News
# Create your views here.

def json_data(req):
	if 'Referer' in req.headers and req.headers['Referer'].startswith("http://127.0.0.1:8000") :
		offset = int(req.GET.get("offset", 0))
		size = int(req.GET.get("size", 1))


		return JsonResponse({
	        "code": 0,
	        "msg": "ok",
	        "datas": [{"id": news.id, "title": news.title}   for news in News.objects.all()[offset: offset+size]]
	    })
	else:
		return JsonResponse({
			"code": -1,
			"msg": "未知错误:请添加referer"
		})


def html_data(req):
	if 'User-Agent' in req.headers:
		if req.headers['User-Agent'] in [
			"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.7049.85 Safari/537.36"
		]:
			return render(req, "main/index.html")

	return HttpResponse("未知错误:只有谷歌可以访问")



def forbiden_ip(req):
	print(req.META.get('REMOTE_ADDR'))
	ip = req.META.get('REMOTE_ADDR')
	if ip in ["192.168.11.160", "192.168.11.158", "192.168.11.130"]:
		return HttpResponse("未知错误")
	return HttpResponse(f'以下IP 被禁用，其他IP正常 {["192.168.11.160", "192.168.11.158", "192.168.11.130"]}')


def forbiden_user(req):
	print(req.COOKIES.get('sessionid'))
	if req.user.username in ["admin"]:
		return HttpResponse("未知错误")
	elif not req.user.is_authenticated:
		return HttpResponse("未知错误")
	else:
		return HttpResponse(f'以下用户 被禁用 admin/匿名用户  只有mcl可以访问')




